using Gee;
using Sqlite;

namespace Meridian.Data {

public class DataRecord {

    private Map<string, DataField> fields;
    
    public DataRecord () {
        fields = new HashMap<string, DataField> ();
    }
    
    public void add_field (string column, Sqlite.Value content) {
        fields.set(column, new DataField (content));
    }
    
    public string get_string (string column) {
        var field = get_field (column);
        return field != null ? field.text : "";
    }
    
    public int get_int (string column) {
        var field = get_field (column);
        return field != null ?  field.number : 0;
    }
    
    public DateTime get_date (string column) {
        var field = get_field (column);
        return field != null ? field.date : new DateTime.now_local ();
    }
    
    private DataField? get_field (string column) {
        if (column in fields.keys) {        
            return fields[column];
        } else {
            debug("Column [" + column + "] not in this record");
            return null;
        }
    }
    
    private class DataField : GLib.Object {
        
        public string text {get; private set;}
        public int number {get; private set;}
        public DateTime date {get; private set;}
        
        public DataField (Sqlite.Value content) {
            this.text = content.to_text ();
            this.number = content.to_int ();
            this.date = new DateTime.from_unix_local(content.to_int64 ());
        }
        
    }

}
}